// pages/Apply_adoption_input/Apply_adoption_input.js
var host = 'https://duolangdang.cn/';


Page({

    /**
     * 页面的初始数据
     */
    data: {
        dates: '2019-10-01',
        marriageArray: [{
            "id": "1",
            "text": "单身"
        }, {
            "id": "2",
            "text": "恋爱期"
        }, {
            "id": "3",
            "text": "已婚已育"
        }, {
            "id": "4",
            "text": "已婚未育"
        }, {
            "id": "5",
            "text": "已婚已孕"
        }],
        liveArray: [{
            "id": "1",
            "text": "产权房"
        }, {
            "id": "2",
            "text": "整租"
        }],
        brands: [],
        objectArray: [{
                brand: "福州",
                id: 0,
                array: ['鼓楼区', '台江区', '仓山区', '马尾区', '晋安区', '福清市', '长乐市', '闽侯县', '连江县', '罗源县', '闽清县', '永泰县']
            },
            {
                brand: "厦门",
                id: 1,
                array: ['思明区', '海沧区', '湖里区', '集美区', '同安区', '翔安区']
            },
            {
                brand: "漳州",
                id: 2,
                array: ['芗城区', '龙文区', '龙海市', '漳浦县', '南靖县', '云霄县', '平和县', '华安县', '长泰县', '诏安县', '东山县']
            },
            {
                brand: "龙岩",
                id: 3,
                array: ['新罗区', '漳平市', '长汀县', '永定县', '上杭县', '武平县', '连城县']
            },
            {
                brand: "宁德",
                id: 4,
                array: ['蕉城区', '东侨新区', '福安市', '福鼎市', '霞浦县', '古田县', '屏南县', '寿宁县', '周宁县', '柘荣县']
            },
            {
                brand: "平潭",
                id: 5,
                array: ['潭城', '苏澳', '流水', '澳前', '北厝', '平原', '敖东']
            },
            {
                brand: "泉州",
                id: 6,
                array: ['丰泽区', '鲤城区', '洛江区', '泉港区', '石狮市', '晋江市', '南安市', '惠安县', '安溪县', '永春县', '德化县', '金门县']
            },
            {
                brand: "三明",
                id: 7,
                array: ['梅列区', '三元区', '永安市', '明溪县', '清流县', '宁化县', '大田县', '尤溪县', '沙县', '将乐县', '泰宁县', '建宁县']
            },
            {
                brand: "莆田",
                id: 8,
                array: ['城厢区', '涵江区', '荔城区', '秀屿区', '仙游县']
            },
            {
                brand: "南平",
                id: 8,
                array: ['延平区', '邵武市', '武夷山市', '建瓯市', '建阳市', '顺昌县', '浦城县', '光泽县', '松溪县', '政和县']
            }
        ],
        object: [],
        brandindex: 0,
        index1: 0,
        incomeArray: [{
            "id": "1",
            "text": "0-2000"
        }, {
            "id": "2",
            "text": "2000-5000"
        }, {
            "id": "3",
            "text": "5000-10000"
        }, {
            "id": "4",
            "text": "10000以上"
        }],
        experArray: [{
            "id": "1",
            "text": "有"
        }, {
            "id": "2",
            "text": "无"
        }],
        currentArray: [{
            "id": "1",
            "text": "有猫"
        }, {
            "id": "2",
            "text": "有狗"
        }, {
            "id": "3",
            "text": "猫狗双全"
        }, {
            "id": "4",
            "text": "暂无"
        }],
        adopt_birthday: "",
        adopt_name: "",
        adopt_phone: "",
        adopt_wechat: "",
        apple_id: "",
        city_address: "",
        cua_address: "",
        current_pets: "",
        income_level: "",
        living_environment: "",
        marriage_code: "",
        message: "",
        pet_experience: "",
        pet_id: "",
        spare_name: "",
        spare_phone: "",
        token: "",
        adopt_state: "审核中",
    },

    //  点击日期组件确定事件  
    bindDateChange: function(e) {
        console.log(e.detail.value)
        this.setData({
            dates: e.detail.value
        })
    },

    marriageArray: function(e) {
        console.log(e.detail.text)
        var that = this;
        that.setData({
            marriage_code: e.detail.text
        });
    },
    message: function(e) {
        var that = this;
        that.setData({
            message: e.detail.value
        })
    },
    liveArray: function(e) {
        console.log(e.detail.text)
        var that = this;
        that.setData({
            living_environment: e.detail.text
        });
    },
    // cityArray: function(e) {
    //     console.log(e.detail.text)
    //     var that = this;
    //     that.setData({
    //         city_address: e.detail.text
    //     });
    // },
    // areaArray: function(e) {
    //     console.log(e.detail.text)
    //     var that = this;
    //     that.setData({
    //         cua_address: e.detail.text
    //     });
    // },
    incomeArray: function(e) {
        console.log(e.detail.text)
        var that = this;
        that.setData({
            income_level: e.detail.id
        });
    },
    experArray: function(e) {
        console.log(e.detail.text)
        var that = this;
        that.setData({
            pet_experience: e.detail.text
        });
    },
    currentArray: function(e) {
        console.log(e.detail.text)
        var that = this;
        that.setData({
            current_pets: e.detail.text
        });
    },
    // message: function(e) {
    //     console.log(e.detail.text)
    //     var that = this;
    //     that.setData({
    //         message: e.detail.text
    //     });
    // },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function(options) {
        console.log(options.pet_id)
        let apple_id = "";
        let token = "";
        apple_id = wx.getStorageSync('openID');
        token = wx.getStorageSync('tokenID')
        this.setData({
            pet_id: options.pet_id,
            apple_id: apple_id,
            token: token
        })
        var objectArray = this.data.objectArray
        var brands = []
        for (var i = 0; i < objectArray.length; i++) {
            brands.push(objectArray[i].brand)
        }
        this.setData({
            brands: brands,
            object: objectArray[this.data.brandindex].array,
            city_address: objectArray[this.data.brandindex].brand,
            cua_address: this.data.objectArray[this.data.brandindex].array[this.data.index1]
        })
    },
    bindPickerChangecity: function(e) {
        this.setData({
            brandindex: e.detail.value,
            index1: 0
        })
        var objectArray = this.data.objectArray
        this.setData({
            object: objectArray[this.data.brandindex].array
        })

        // console.log(this.data.brands[this.data.brandindex])
        this.setData({
            city_address: this.data.brands[this.data.brandindex]
        })
        console.log(this.data.city_address)

        // console.log(this.data.brandindex)
        //     console.log(objectArray[this.data.brandindex].array[this.data.index1])
    },
    bindPickerChangearea: function(e) {
        this.setData({
            index1: e.detail.value
        })
        this.setData({
                cua_address: this.data.objectArray[this.data.brandindex].array[this.data.index1]
            })
            // console.log(this.data.objectArray[this.data.brandindex].array[this.data.index1])
        console.log(this.data.cua_address)
    },

    adopt_name: function(e) {
        this.setData({
            adopt_name: e.detail.value
        })
    },
    adopt_wechat: function(e) {
        this.setData({
            adopt_wechat: e.detail.value
        })
    },
    adopt_phone: function(e) {
        this.setData({
            adopt_phone: e.detail.value
        })
    },
    spare_name: function(e) {
        this.setData({
            spare_name: e.detail.value
        })
    },
    spare_phone: function(e) {
        this.setData({
            spare_phone: e.detail.value
        })
    },
    // adopt_birthday: function(e) {
    //     this.setData({
    //         adopt_birthday: e.detail.value
    //     })
    // },

    make: function() {
        let that = this;
        let adoptM = {
            // "adopt_birthday": "2019-7-20",
            // "adopt_name": "张三",
            // "adopt_phone": 18359926826,
            // "adopt_state": "string",
            // "adopt_wechat": "c122355283",
            // "apple_id": "oIxLF5PubOflqkJfjG4_9x-qIIWk",
            // "city_address": "福州",
            // "cua_address": "鼓楼区",
            // "current_pets": "有猫",
            // "income_level": 0,
            // "living_environment": "产权房",
            // "marriage_code": "单身",
            // "message": "string",
            // "pet_experience": "有",
            // "pet_id": 31,
            // "spare_name": "李四",
            // "spare_phone": 13023869093
        };
        adoptM.adopt_birthday = that.data.dates;
        adoptM.adopt_name = that.data.adopt_name;
        adoptM.adopt_phone = that.data.adopt_phone;
        adoptM.adopt_wechat = that.data.adopt_wechat;
        adoptM.apple_id = that.data.apple_id;
        adoptM.city_address = that.data.city_address;
        adoptM.cua_address = that.data.cua_address;
        adoptM.current_pets = that.data.current_pets;
        adoptM.income_level = that.data.income_level;
        adoptM.living_environment = that.data.living_environment;
        adoptM.marriage_code = that.data.marriage_code;
        adoptM.message = that.data.message;
        adoptM.pet_id = that.data.pet_id;
        adoptM.pet_experience = that.data.pet_experience;
        adoptM.spare_name = that.data.spare_name;
        adoptM.spare_phone = that.data.spare_phone;
        adoptM.adopt_state = that.data.adopt_state;
        console.log(adoptM)
        return adoptM
    },
    sub: function() {
        var that = this;
        let m = this.make()
        wx.showModal({
            title: '提示',
            content: '确认是否提交',
            success: function(res) {
                if (res.confirm) {
                    wx.showLoading({
                        title: '正在提交申请',
                    })

                    console.log(m)
                    if (that.data.dates === "" && that.data.adopt_name === "" && that.data.adopt_phone === "" && that.data.adopt_wechat === "" && that.data.city_address === "" && that.data.cua_address === "" && that.data.current_pets === "" && that.data.income_level === null && that.data.living_environment === "" && that.data.marriage_code === "" && that.data.message === "" && that.data.pet_experience === "" && that.data.spare_name === "" && that.data.spare_phone === "") {
                        wx.hideLoading();
                        wx.showModal({
                            title: "温馨提示",
                            content: "请将信息填写完整",
                            success: function(res) {
                                if (res.confirm) {

                                } else if (res.cancel) {

                                }

                            }
                        })
                    } else {
                        wx.request({
                            //项目的真正接口，通过字符串拼接方式实现
                            url: host + "Adopt/applyAdoptPet",
                            header: {
                                "content-type": "application/json;charset=UTF-8",
                                "x-auth-token": that.data.token,
                            },
                            data: m,
                            method: 'POST',
                            success: function(res) {
                                //参数值为res.data,直接将返回的数据传入
                                //   wx.setStorageSync('mm', uu);
                                if (res.data.message === "操作成功") {
                                    wx.hideLoading();
                                    console.log(res)
                                    wx.showToast({
                                        title: '提交成功',
                                        icon: 'success', //如果要纯文本，不要icon，将值设为'none'
                                        duration: 1500
                                    })
                                    setTimeout(() => {
                                        wx.navigateBack({
                                            delta: 3, // 回退前 delta(默认为1) 页面
                                        })
                                    }, 1500)
                                } else {
                                    wx.hideLoading();
                                    console.log(res)
                                    wx.showToast({
                                        title: '提交失败',
                                        icon: 'none', //如果要纯文本，不要icon，将值设为'none'
                                        duration: 3000
                                    })
                                }
                            },
                            fail: function() {

                            },
                        })
                    }


                } else if (res.cancel) {
                    console.log('用户点击取消')
                }
            }
        })

    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function() {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function() {

    }
})